package com.intelligentcar.springboot.service.serverImpl;

import com.intelligentcar.springboot.bean.*;
import com.intelligentcar.springboot.mapper.TotalMapper;
import com.intelligentcar.springboot.service.TotalServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TotalServerImpl implements TotalServer {
    @Autowired
    private TotalMapper totalMapper;

    private Map<String,Object> map = new HashMap<>();
    private List<Integer> totalList = new ArrayList<>();
    private List<Total> totals = new ArrayList<>();

    @Override
    public Map<String,Object> lineTotal() {
        map.clear();
        totalList.clear();
        List<String> list = totalMapper.lineArea();
        int lineTotals = totalMapper.lineTotals();
        for (String t : list) {
            int lines = totalMapper.lineTotal(t);
            totalList.add(lines);
        }
        map.put("lines",list);
        map.put("lineList",totalList);
        map.put("lineTotal",lineTotals);
        return map;
    }

    @Override
    public Map<String, Object> stationTotal() {
        map.clear();
        totalList.clear();
        List<String> list = totalMapper.stationArea();
        int stationTotals = totalMapper.stationTotals();
        for (String t : list) {
            int stations = totalMapper.stationTotal(t);
            totalList.add(stations);
        }
        map.put("stations",list);
        map.put("stationList",totalList);
        map.put("stationTotal",stationTotals);
        return  map;
    }

    @Override
    public Map<String, Object> driverTotal(Driverattendance driverattendance) {
        map.clear();
        totals.clear();
        String toDay =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        driverattendance.setDate(toDay);
        LeaveRequest leaveRequest = new LeaveRequest();
        leaveRequest.setBeginDate(driverattendance.getBeginDate());
        leaveRequest.setEndDate(driverattendance.getEndDate());
        leaveRequest.setLeaveTime(toDay);
        int driverCount = totalMapper.selectDriverCount();
        int driverLeaveTime = totalMapper.selectLeaveTimeDriver(leaveRequest);
        int driverIntime = totalMapper.selectIntimeDriver(driverattendance);
        int outTime = driverCount - driverIntime - driverLeaveTime;

        Total total1 = new Total();
        total1.setValue(String.valueOf(driverIntime));
        total1.setName("司机考勤数");
        totals.add(total1);

        Total total2 = new Total();
        total2.setValue(String.valueOf(driverLeaveTime));
        total2.setName("司机请假数");
        totals.add(total2);

        Total total3 = new Total();
        total3.setValue(String.valueOf(outTime));
        total3.setName("司机缺勤数");
        totals.add(total3);

        map.put("driverCount",driverCount);
        map.put("driverList",totals);
        return  map;
    }

    @Override
    public Map<String, Object> staffTotal(StaffAttendance staffAttendance) {
        map.clear();
        totals.clear();
        String toDay =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        staffAttendance.setDate(toDay);
        int staffCount = totalMapper.selectStaffCount();
        int staffLeaveTime = 0;
        int driverIntime = totalMapper.selectIntimeStaff(staffAttendance);
        int outTime = staffCount - driverIntime - staffLeaveTime;

        Total total1 = new Total();
        total1.setValue(String.valueOf(driverIntime));
        total1.setName("工作人员考勤数");
        totals.add(total1);

        Total total2 = new Total();
        total2.setValue(String.valueOf(staffLeaveTime));
        total2.setName("工作人员请假数");
        totals.add(total2);

        Total total3 = new Total();
        total3.setValue(String.valueOf(outTime));
        total3.setName("工作人员缺勤数");
        totals.add(total3);

        map.put("staffCount",staffCount);
        map.put("staffList",totals);
        return  map;
    }


    @Override
    public Map<String, Object> topTotal(DataPersonTest dataPersonTest) {
        totalList.clear();
        map.clear();
        String toDay =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<String> stations = totalMapper.selectAllStation();
        if (stations.size()>0){
            for (String d: stations) {
                DataPersonTest dataPersonTest1 = new DataPersonTest();
                dataPersonTest1.setUpCarDate(toDay);
                dataPersonTest1.setUpStation(d);
                dataPersonTest1.setBeginDate(dataPersonTest.getBeginDate());
                dataPersonTest1.setEndDate(dataPersonTest.getEndDate());
                List<Integer> list = totalMapper.selectPersonBy(dataPersonTest1);
                int num = 0;
                for (Integer i: list) {
                    num = num + i;
                }
                totalList.add(num);
            }
        }
        map.put("tops", stations);
        map.put("topList", totalList);
        return map;
    }

    @Override
    public Map<String, Object> falutTotal(Falut falut) {
        totalList.clear();
        map.clear();
        String toDay =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<String> stations = totalMapper.selectAllStation();
        if (stations.size()>0){
            for (String d: stations) {
               Falut falut1 = new Falut();
                falut1.setDate(toDay);
                falut1.setStation(d);
                falut1.setBeginDate(falut.getBeginDate());
                falut1.setEndDate(falut.getEndDate());
                int falutNum = totalMapper.selectFalutBy(falut1);
                totalList.add(falutNum);
            }
        }
        map.put("faluts", stations);
        map.put("falutList", totalList);
        return map;
    }
}
